package hotel;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.swing.JOptionPane;

import com.sun.rowset.CachedRowSetImpl;

public class Autenticazione {
	private Database d;
	private String username, password, email, telefono;
	public final int UTENTE_SI = 1;
	public final int UTENTE_NO = 2;
	public final int PASS_NO = 3;
	public final int NON_CONNESSO = 4;

	public Autenticazione(String username, char[] pass, Database d)
			throws SQLException, IOException {
		this.d = d;
		this.username = username;
		String ps = "";
		for (int i = 0; i < pass.length; i++)
			ps = ps + pass[i];
		this.password = ps;
	}

	public Autenticazione(String username, char[] pass, String email,
			String telefono, Database d) {
		this.d = d;
		this.username = username;
		String ps = "";
		for (int i = 0; i < pass.length; i++)
			ps = ps + pass[i];
		this.password = ps;
		this.email = email;
		this.telefono = telefono;
	}

	public boolean setUsername() throws SQLException, ClassNotFoundException,
			IOException {
		MyResultSet rs = d
				.querySelect("Select username from accesso.dati");
		while (rs.next()) {
			if (rs.getString(1).equals(username))
				return false;
		}
		d.queryInserimento("insert into accesso.dati(username) values("
				+ username + ")");
		return true;
	}

	public boolean setPassword() throws SQLException, IOException {
		d.queryInserimento("update accesso.dati set pass='" + password
				+ "' where username='" + username + "'");
		return true;
	}

	public boolean setEmail() throws SQLException, IOException {
		d.queryInserimento("update accesso.dati set email='" + email
				+ "' where username='" + username + "'");
		return true;
	}

	public boolean setTelefono() throws SQLException, IOException {
		d.queryInserimento("update accesso.dati set telefono='" + telefono
				+ "' where username='" + username + "'");
		return true;
	}

	public void createAccount() throws IOException {
		d.queryInserimento("insert into accesso.dati (username,pass,email,telefono) values ('"
				+ username
				+ "','"
				+ password
				+ "','"
				+ email
				+ "','"
				+ telefono + "')");
	}

	public int esiste() throws SQLException, ClassNotFoundException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException{
		return d.effettuaLogin(username, password);
	}
	
	public boolean passRecuperabile() throws ClassNotFoundException,
			IOException, SQLException {
		MyResultSet rs = d
				.querySelect("select email from accesso.dati where username='"
						+ username + "'");
		if (rs.next()) {
			if (rs.getString(1).equals(email))
				return true;
		}
		return false;
	}

	public boolean userRecuperabile(String email)
			throws ClassNotFoundException, IOException, SQLException {
		MyResultSet rs = d
				.querySelect("select email from accesso.dati where pass='"
						+ password + "'");
		if (rs.next()) {
			if (email.equals(rs.getString(1)))
				return true;
		}
		return false;
	}

	public String getUsername() throws ClassNotFoundException, IOException,
			SQLException {
		MyResultSet rs = d
				.querySelect("select username from accesso.dati where pass='"
						+ password + "'");
		if (rs.next()) {
			return rs.getString(1);
		}
		return null;
	}

	public String getPassword() throws ClassNotFoundException, IOException,
			SQLException {
		MyResultSet rs = d
				.querySelect("select pass from accesso.dati where username='"
						+ username + "'");
		if (rs.next()) {
			return rs.getString(1);
		}
		return null;
	}

	public String getEmail() throws ClassNotFoundException, IOException,
			SQLException {
		MyResultSet rs = d
				.querySelect("select email from accesso.dati where pass='"
						+ password + "'");
		if (rs.next()) {
			return rs.getString(1);
		}
		return null;
	}

	public String getTelefono() throws ClassNotFoundException, IOException,
			SQLException {
		MyResultSet rs = d
				.querySelect("select email from accesso.dati where pass='"
						+ password + "'");
		if (rs.next()) {
			return rs.getString(1);
		}
		return null;
	}

	public int esisteUser() throws ClassNotFoundException, IOException,
			SQLException {
		MyResultSet rs = d
				.querySelect("select username from accesso.dati where username='"
						+ username + "'");
		if (rs.next()) {
			return UTENTE_SI;
		}
		return UTENTE_NO;
	}
}
